package com.example.add8_17.fragment;


import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.add8_17.R;
import com.example.add8_17.adapter.Sqleadapter;
import com.example.add8_17.bean.Sqlbean;
import com.example.add8_17.sql.Sql;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class SqlFragment extends Fragment {


    private RecyclerView mSqlRe;
    private ArrayList<Sqlbean> sqlbeans;
    private Sqleadapter sqleadapter;
    private Sql sql;

    public SqlFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_sql, container, false);
        initViews(view);
        return view;
    }

    private void initViews(View view) {
        mSqlRe = view.findViewById(R.id.sql_re);
        mSqlRe.setLayoutManager(new LinearLayoutManager(getContext()));
        mSqlRe.addItemDecoration(new DividerItemDecoration(getContext(), LinearLayout.VERTICAL));
        sqlbeans = new ArrayList<>();
        sql = new Sql(getContext());
        List<Sqlbean> namequery = sql.namequery();
        sqlbeans.addAll(namequery);
        sqleadapter = new Sqleadapter(getContext(), sqlbeans);
        mSqlRe.setAdapter(sqleadapter);
        initlistener();
    }

    private void initlistener() {
        sqleadapter.setOnContextClickListener(new Sqleadapter.OnContextClickListener() {
            @Override
            public void oncontext(View v, int position) {
                int id = sqlbeans.get(position).getId();
                initpop(v,id);
            }
        });
    }

    private void initpop(View v,int id) {
        View inflate = LayoutInflater.from(getContext()).inflate(R.layout.sql_pop, null);
        PopupWindow popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable());
        popupWindow.showAsDropDown(v,0,0);
        Button bu = inflate.findViewById(R.id.pop_bu);
        bu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sql.namedelete(id);
                List<Sqlbean> namequery = sql.namequery();
                sqlbeans.clear();
                sqlbeans.addAll(namequery);
                sqleadapter.notifyDataSetChanged();
                popupWindow.dismiss();
            }
        });
    }
}
